#include "test_i2c.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_iomux.h"
#include "string.h"


void cm_test_i2c_operation(unsigned char *cmd[],int len)
{
    if(0 == strcmp("open",cmd[2]))
    {
        cm_i2c_dev_e dev = atoi(cmd[3]);
        cm_i2c_addr_type_e addr_type = atoi(cmd[4]);//0   /*!< 地址类型，7bit或10bit*//*!仅7bit地址*/
        cm_i2c_mode_e  mode = atoi(cmd[5]);//0
        int clk = atoi(cmd[6]);//100000,400000
        cm_i2c_cfg_t cfg = {
            addr_type,   /*!< 地址类型，7bit或10bit*//*!仅7bit地址*/
            mode,
            clk,                        /*!< 时钟频率*/
        };

#if CM_OPENCPU_MODEL_ML307A
    if(0 == dev)
    {
        cm_iomux_set_pin_func(CM_IOMUX_PIN_66,CM_IOMUX_FUNC_FUNCTION1);
        cm_iomux_set_pin_func(CM_IOMUX_PIN_67,CM_IOMUX_FUNC_FUNCTION1);
    }
    if(1 == dev)
    {
        cm_iomux_set_pin_func(CM_IOMUX_PIN_52,CM_IOMUX_FUNC_FUNCTION2);
        cm_iomux_set_pin_func(CM_IOMUX_PIN_53,CM_IOMUX_FUNC_FUNCTION2);
    }
    if(2 == dev)
    {
        cm_iomux_set_pin_func(CM_IOMUX_PIN_74,CM_IOMUX_FUNC_FUNCTION2);
        cm_iomux_set_pin_func(CM_IOMUX_PIN_75,CM_IOMUX_FUNC_FUNCTION2);
    }
#endif

#if CM_OPENCPU_MODEL_ML305A
    if(0 == dev)
    {
        cm_iomux_set_pin_func(CM_IOMUX_PIN_11,CM_IOMUX_FUNC_FUNCTION1);
        cm_iomux_set_pin_func(CM_IOMUX_PIN_12,CM_IOMUX_FUNC_FUNCTION1);
    }
    if(1 == dev)
    {
        cm_iomux_set_pin_func(CM_IOMUX_PIN_56,CM_IOMUX_FUNC_FUNCTION1);
        cm_iomux_set_pin_func(CM_IOMUX_PIN_57,CM_IOMUX_FUNC_FUNCTION1);
    }
    if(2 == dev)
    {
        cm_iomux_set_pin_func(CM_IOMUX_PIN_65,CM_IOMUX_FUNC_FUNCTION2);
        cm_iomux_set_pin_func(CM_IOMUX_PIN_67,CM_IOMUX_FUNC_FUNCTION2);
    }
#endif
        int32_t i2c_open = cm_i2c_open(dev,&cfg);
        cm_demo_printf("i2c cm_i2c_open ret:%d\n",i2c_open);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,i2c_open,cm_i2c_open);
    }else if(0 == strcmp("close",cmd[2]))
    {
        cm_i2c_dev_e dev = atoi(cmd[3]);
        int32_t i2c_close = cm_i2c_close( dev);
        cm_demo_printf("i2c cm_i2c_close ret:%d\n",i2c_close);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,i2c_close,cm_i2c_close);
    }else if(0 == strcmp("write",cmd[2]))
    {
        cm_i2c_dev_e dev = atoi(cmd[3]);
        uint16_t slave_addr = atoi(cmd[4]);
        uint8_t data[12] = {0};
        data[0] = 0x01;
        data[1] = 0x00;
        strcpy(&data[2],cmd[5]);
        int32_t len = atoi(cmd[6]);
        int32_t i2c_write = cm_i2c_write( dev, slave_addr, data, len);
        cm_demo_printf("i2c cm_i2c_write ret:%d\n",i2c_write);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,i2c_write,cm_i2c_write);
    }else if(0 == strcmp("read",cmd[2]))
    {
        cm_i2c_dev_e dev = atoi(cmd[3]);
        uint16_t slave_addr = atoi(cmd[4]);
        char data[1000] = {0};
        int32_t len = atoi(cmd[5]);
        int32_t i2c_read = cm_i2c_read(dev,slave_addr,data,len);
        cm_demo_printf("i2c cm_i2c_read ret:%d\n",i2c_read);
        cm_demo_printf("read data: %s\n",data);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,i2c_read,cm_i2c_read);
    }else{
        cm_demo_printf("param error!\n");
    }
}
